﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.ServiceLocation.Fakes;

using Happy.Event;
using Happy.Event.Test.Stub.Simple;

namespace Happy.Event.Test
{
    [TestClass]
    public class EventPublisherTest
    {
        [TestMethod]
        public void Publish_Simple_Test()
        {
            this.Fake_Simple_ServiceLocator();

            var testEvent = new TestEvent();

            EventPublisher.Current.Publish(testEvent);

            Assert.IsTrue(testEvent.Output.Contains(typeof(TestSyncEventSubscriber1).Name));
            Assert.IsTrue(testEvent.Output.Contains(typeof(TestSyncEventSubscriber2).Name));
            Assert.IsTrue(testEvent.Output.Contains(typeof(TestEventInterceptor1Attibute).Name));
            Assert.IsTrue(testEvent.Output.Contains(typeof(TestEventInterceptor2Attibute).Name));
            Assert.IsTrue(testEvent.Output.StartsWith("Before:" + typeof(TestEventInterceptor1Attibute).Name));
            Assert.IsTrue(testEvent.Output.Contains("After:" + typeof(TestEventInterceptor1Attibute).Name));
        }

        private void Fake_Simple_ServiceLocator()
        {
            var serviceLocator = new StubIServiceLocator();

            ServiceLocator.SetLocatorProvider(() => serviceLocator);

            serviceLocator
                .GetAllInstancesOf1<ISyncEventSubscriber<TestEvent>>(
                    () =>
                    {
                        return new List<ISyncEventSubscriber<TestEvent>>
                        {
                            new TestSyncEventSubscriber1(),
                            new TestSyncEventSubscriber2()
                        };
                    }
                );

            serviceLocator
                .GetAllInstancesOf1<IAsyncEventSubscriber<TestEvent>>(
                    () =>
                    {
                        return new List<IAsyncEventSubscriber<TestEvent>>
                        {
                            new TestAsyncEventSubscriber1(),
                            new TestAsyncEventSubscriber2()
                        };
                    }
                );
        }
    }
}
